package appClientModule;

/**
 * Project Name:myapplet 
 * File Name:Tlv.java 
 * Package Name: 
 * Date:2017Äê8ÔÂ21ÈÕÉÏÎç9:13:42 
 * Copyright (c) 2017, liuhh@hengbao.com All Rights Reserved. 
 * 
*/

/** 
 * ClassName:Tlv <br/> 
 * Function: TODO ADD FUNCTION. <br/> 
 * Reason:   TODO ADD REASON. <br/> 
 * Date:     2017Äê8ÔÂ21ÈÕ ÉÏÎç9:13:42 <br/> 
 * @author   liuhh 
 * @version   
 * @since    JDK 1.8 
 * @see       
 */
public class Tlv {
	public String t;
	public String l;
	public String v;
	public boolean flag;
	public int index;
	public boolean noShorMsg;
	
	public String getT() {
		return t;
	}
	public void setT(String t) {
		this.t = t;
	}
	public String getL() {
		return l;
	}
	public void setL(String l) {
		this.l = l;
	}
	public String getV() {
		return v;
	}
	public void setV(String v) {
		this.v = v;
	}
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	
	public boolean isNoShorMsg() {
		return noShorMsg;
	}
	public void setNoShorMsg(boolean noShorMsg) {
		this.noShorMsg = noShorMsg;
	}

	/**解析标签*/
	public Tlv parser(String msg) {
		Integer currentIndex = 0 ;
		Tlv tlv = null;
		boolean flag = true;
		while(flag){
			//System.out.println(currentIndex);
			tlv = deail(msg,currentIndex);
			flag = tlv.isFlag();
			currentIndex = tlv.index;
		}
		if(Integer.parseInt(tlv.getL(),16)*2 != tlv.getV().length()){
			//System.out.println("Êý¾Ý½âÎö´íÎó");
			return null;
		}
		return tlv;
	}

	public Tlv deail(String msg , Integer index){
		String t = msg.substring(index,index+=2);
		String l = msg.substring(index,index+=2);
		String v = msg.substring(index,index+=Integer.parseInt(l,16)*2);
		Tlv tlv = add(t,l,v,index);
		//System.out.println(t+l+v);
		if(t.equals("48")){
			int cindex = 0;
			msg = v;
			String sms_ptp = msg.substring(cindex,cindex+=2);//SMS Point-to-Point
			//System.out.println("sms_ptp:"+sms_ptp);
			String ti = msg.substring(cindex,cindex+=8);//Teleservice Identifier
			//System.out.println("Teleservice Identifier:"+ti);
			String r_addr = msg.substring(cindex,cindex+=16);//Ä¿µÄµØÖ·
			//System.out.println("Ä¿µÄµØÖ·"+r_addr);
			tlv.setIndex(cindex);
			tlv = deail(msg,cindex);
		}
		if(t.equals("01")){			
			tlv.setFlag(false);
		}
		if(t.equals("08")){
			int dindex = 0;
			msg = v;
			String msg_identifier = msg.substring(dindex,dindex+=10);//Message Identifier
			//System.out.println(msg_identifier);
			tlv.setIndex(dindex);
			tlv = deail(msg,dindex);
		}
		if(t.equals("81")) {
			if(!v.substring(2,4).equals("13")) {
				tlv.setFlag(false);
				tlv.setNoShorMsg(true);
			}else {
				tlv.setFlag(true);
				tlv.setNoShorMsg(false);
			}
		}
		return tlv;
	}
	private Tlv add(String t2, String l2, String v2,Integer index) {
		Tlv tlv = new Tlv();
		tlv.setT(t2);
		tlv.setL(l2);
		tlv.setV(v2);
		tlv.setFlag(true);
		tlv.setIndex(index);
		return tlv;
	}

	/**下载短信标签解析*/
	public Tlv parserDownMsg(String msg) {
		Integer currentIndex = 0 ;
		Tlv tlv = null;
		boolean flag = true;
		while(flag){
		//	//System.out.println("µ±Ç°½ø¶È:"+currentIndex);
			tlv = deail2(msg,currentIndex);
			flag = tlv.isFlag();
		//	//System.out.println("ÒÑÖª:"+tlv.index);
			currentIndex = tlv.index;
			
		}
		if(Integer.parseInt(tlv.getL(),16)*2 != tlv.getV().length()){
			//System.out.println("Êý¾Ý½âÎö´íÎó");
			return null;
		}
		return tlv;
	}
	public Tlv deail2(String msg , Integer index){
	//	//System.out.println("deail2-index:"+index);
		String t = msg.substring(index,index+=2);
		String l = msg.substring(index,index+=2);
		String v = msg.substring(index,index+=Integer.parseInt(l,16)*2);
		Tlv tlv = add(t,l,v,index);
		//System.out.println(t+l+v);
		if(tlv.getT().equals("48")){
			int cindex = 0;
			msg = v;
			String sms_ptp = msg.substring(cindex,cindex+=2);//SMS Point-to-Point
			//System.out.println("sms_ptp:"+sms_ptp);
			String ti = msg.substring(cindex,cindex+=8);//Teleservice Identifier
			//System.out.println("Teleservice Identifier:"+ti);
			String r_addr = msg.substring(cindex,cindex+=16);//Ä¿µÄµØÖ·
			//System.out.println("Ä¿µÄµØÖ·"+r_addr);
			tlv.setIndex(cindex);
			tlv = deail2(msg,cindex);
		}
		if(tlv.getT().equals("01")){			
			tlv.setFlag(false);
		}
		if(tlv.getT().equals("06")){
			index = tlv.getIndex();
			t = msg.substring(index,index+=2);
			l = msg.substring(index,index+=2);
			v = msg.substring(index,index+=Integer.parseInt(l,16)*2);
			tlv = add(t,l,v,index);
		}
		if(tlv.getT().equals("08")) {
			int findex = 0;
			msg = v;
			t = msg.substring(findex,findex+=2);
			l = msg.substring(findex,findex+=2);
			v = msg.substring(findex,findex+=Integer.parseInt(l,16)*2);
			tlv = add(t,l,v,findex);
		}
		if(tlv.getT().equals("00")) {
			index = tlv.getIndex();
			t = msg.substring(index,index+=2);
			l = msg.substring(index,index+=2);
			v = msg.substring(index,index+=Integer.parseInt(l,16)*2);
			tlv = add(t,l,v,index);
		}
		return tlv;
	}	
}
  